package jgl.vajra.management.part.order.activity;

import androidx.recyclerview.widget.LinearLayoutManager;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import jgl.vajra.management.R;
import jgl.vajra.management.bean.OpenCityBean;
import jgl.vajra.management.databinding.ActivityReceivingAreaBinding;
import jgl.vajra.management.event.OpenCityEvent;
import jgl.vajra.management.part.order.contract.ReceivingAreaContract;
import jgl.vajra.management.part.order.viewModel.ReceivingAreaViewModel;
import jgl.vajra.management.utils.RandomUtils;
import jgl.vajra.management.widget.databindingadapter.BaseBindingItemPresenter;
import jgl.vajra.management.widget.databindingadapter.SingleTypeBindingAdapter;
import jgl.vajra.management.widget.mvvm.factory.CreateViewModel;
import jgl.vajra.management.widget.mvvm.view.BaseMVVMActivity;
import jgl.vajra.management.widget.retrofithelper.utils.AEACCoder;
import jgl.vajra.management.widget.utils.ToastUtils;

@CreateViewModel(ReceivingAreaViewModel.class)
public class ReceivingAreaActivity extends BaseMVVMActivity<ReceivingAreaViewModel, ActivityReceivingAreaBinding> implements BaseBindingItemPresenter<OpenCityBean.ProvinceListBean.CityListBean.AreaListBean>, ReceivingAreaContract.View {

    private SingleTypeBindingAdapter adapter;
    private SingleTypeBindingAdapter cityAdapter;
    private SingleTypeBindingAdapter areaAdapter;
    private OpenCityBean openCityBean;
    private int provincePosition;
    private int cityPosition;
    private String name;
    private int provinceId=-1;//省Id
    private int cityId=-1;//城市Id
    private int areaId=-1;//区Id

    @Override
    public int getLayoutId() {
        return R.layout.activity_receiving_area;
    }

    @Override
    public void initView() {
        super.initView();
        mBinding.setView(this);

        // 不可嵌套滑动
        mBinding.recyclerView.setNestedScrollingEnabled(false);
        // 取消head 的 recycleView的焦点
        mBinding.recyclerView.setFocusableInTouchMode(false);
        //设置布局管理器
        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new SingleTypeBindingAdapter(this, null, R.layout.item_receiving_area_province);
        mBinding.recyclerView.setAdapter(adapter);
        adapter.setItemPresenter(this);

        // 不可嵌套滑动
        mBinding.recyclerViewCity.setNestedScrollingEnabled(false);
        // 取消head 的 recycleView的焦点
        mBinding.recyclerViewCity.setFocusableInTouchMode(false);
        //设置布局管理器
        mBinding.recyclerViewCity.setLayoutManager(new LinearLayoutManager(this));
        cityAdapter = new SingleTypeBindingAdapter(this, null, R.layout.item_receiving_area_city);
        mBinding.recyclerViewCity.setAdapter(cityAdapter);
        cityAdapter.setItemPresenter(this);

        // 不可嵌套滑动
        mBinding.recyclerViewArea.setNestedScrollingEnabled(false);
        // 取消head 的 recycleView的焦点
        mBinding.recyclerViewArea.setFocusableInTouchMode(false);
        //设置布局管理器
        mBinding.recyclerViewArea.setLayoutManager(new LinearLayoutManager(this));
        areaAdapter = new SingleTypeBindingAdapter(this, null, R.layout.item_receiving_area);
        mBinding.recyclerViewArea.setAdapter(areaAdapter);
        areaAdapter.setItemPresenter(this);

        requestNetData();
    }

    private void requestNetData() {
        Map<String, Object> map = new HashMap<>();
        map.put("Authorization", AEACCoder.encrypt(RandomUtils.getRandom()));//随机数

        mViewModel.getOpenCityData(map);
    }

    @Override
    public void returnOpenCityData(OpenCityBean openCityBean) {
        this.openCityBean = openCityBean;

       // openCityBean.provinceList.get(0).isClick = true;
       // openCityBean.provinceList.get(0).cityList.get(0).isClick = true;
       // openCityBean.provinceList.get(0).cityList.get(0).areaList.get(0).isClick = true;
       // areaId =openCityBean.provinceList.get(0).cityList.get(0).areaList.get(0).id;
       // name =openCityBean.provinceList.get(0).cityList.get(0).areaList.get(0).name;

        adapter.refresh(openCityBean.provinceList);
        cityAdapter.refresh(openCityBean.provinceList.get(0).cityList);
        areaAdapter.refresh(openCityBean.provinceList.get(0).cityList.get(0).areaList);
    }

    //选择省份
    @Override
    public void onChoiceProvince(int position, OpenCityBean.ProvinceListBean itemData) {
        if (itemData.isClick) return;

        for (int i = 0; i < openCityBean.provinceList.size(); i++) {
            openCityBean.provinceList.get(i).isClick = false;
            for (int j = 0; j < openCityBean.provinceList.get(i).cityList.size(); j++) {
                openCityBean.provinceList.get(i).cityList.get(j).isClick = false;
                for (int x = 0; x < openCityBean.provinceList.get(i).cityList.get(j).areaList.size(); x++) {
                    openCityBean.provinceList.get(i).cityList.get(j).areaList.get(x).isClick = false;
                }
            }
        }

        provincePosition = position;
        openCityBean.provinceList.get(position).isClick = true;
        provinceId =itemData.id;


       // openCityBean.provinceList.get(position).cityList.get(0).isClick = true;
       // openCityBean.provinceList.get(position).cityList.get(0).areaList.get(0).isClick = true;
      //  areaId =openCityBean.provinceList.get(position).cityList.get(0).areaList.get(0).id;
      //  name =openCityBean.provinceList.get(position).cityList.get(0).areaList.get(0).name;
        adapter.refresh();

        cityAdapter.refresh(itemData.cityList);
        areaAdapter.refresh(itemData.cityList.get(0).areaList);
    }

    //城市
    @Override
    public void onChoiceArea(int position, OpenCityBean.ProvinceListBean.CityListBean itemData) {
        if (provinceId==-1){
            ToastUtils.showShort("请先选择省份");
            return;
        }

        if (itemData.isClick) return;
        for (int i = 0; i < openCityBean.provinceList.get(provincePosition).cityList.size(); i++) {
            openCityBean.provinceList.get(provincePosition).cityList.get(i).isClick = false;
            for (int j = 0; j < openCityBean.provinceList.get(provincePosition).cityList.get(i).areaList.size(); j++) {
                openCityBean.provinceList.get(provincePosition).cityList.get(i).areaList.get(j).isClick = false;
            }
        }
        cityPosition = position;
        cityId =itemData.id;
        openCityBean.provinceList.get(provincePosition).cityList.get(position).isClick = true;
      //  openCityBean.provinceList.get(provincePosition).cityList.get(position).areaList.get(0).isClick = true;
      //  areaId =openCityBean.provinceList.get(provincePosition).cityList.get(position).areaList.get(0).id;
      //  name =openCityBean.provinceList.get(provincePosition).cityList.get(position).areaList.get(0).name;
        cityAdapter.refresh();
        areaAdapter.refresh(itemData.areaList);
    }

    //重置
    @Override
    public void onReset() {
        EventBus.getDefault().post(new OpenCityEvent("", "所有区域"));
        finish();
    }

    //确认
    @Override
    public void onDetermine() {
        EventBus.getDefault().post(new OpenCityEvent("and c.urban_id = "+areaId, name));
        finish();
    }

    @Override
    public void onItemClick(int position, OpenCityBean.ProvinceListBean.CityListBean.AreaListBean itemData) {
        if (itemData.isClick) return;
        for (int i = 0; i < openCityBean.provinceList.get(provincePosition).cityList.get(cityPosition).areaList.size(); i++) {
            openCityBean.provinceList.get(provincePosition).cityList.get(cityPosition).areaList.get(i).isClick = false;
        }

        openCityBean.provinceList.get(provincePosition).cityList.get(cityPosition).areaList.get(position).isClick = true;

        areaId =itemData.id;
        name =itemData.name;
        areaAdapter.refresh();
    }
}
